package com.ywps.vaas.framework.util

import com.ywps.vaas.framework.conf.ConfigTools
import com.ywps.vaas.framework.constant.PropertiesConstant
import org.apache.commons.pool2.impl.GenericObjectPoolConfig
import redis.clients.jedis.{HostAndPort, Jedis, JedisCluster}

import java.util
import scala.collection.JavaConverters
import scala.collection.immutable.Set

object RedisClientUtil {
  val properties = ConfigTools.getPropertiesFactory()
  //创建连接池
  var cluster:JedisCluster=_
  var nodes:util.Set[HostAndPort]=_
  var config:GenericObjectPoolConfig[Jedis] =_

  init()

  private def init() = {
    config = new GenericObjectPoolConfig[Jedis]()
    //最大连接数
    config.setMaxTotal(properties.getProperty(PropertiesConstant.REDIS_MAXCON).toInt)
    //最大空闲数
    config.setMaxIdle(properties.getProperty(PropertiesConstant.REDIS_MAXIDLE).toInt)
    val hostAndPort1 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"1"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPort2 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"2"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPort3 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"3"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPort4 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"4"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPort5 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"5"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPort6 = new HostAndPort(properties.getProperty(PropertiesConstant.REDIS_HOST+"6"), properties.getProperty(PropertiesConstant.REDIS_PORT).toInt)
    val hostAndPorts: Predef.Set[HostAndPort] = Set[HostAndPort](hostAndPort1, hostAndPort2, hostAndPort3, hostAndPort4, hostAndPort5, hostAndPort6)
    nodes = JavaConverters.setAsJavaSetConverter[HostAndPort](hostAndPorts).asJava
  }

  def set(key: String, value: String): Unit = {
    try {
      val pwd:String = properties.getProperty(PropertiesConstant.REDIS_PASSWORD)
        pwd match {
          case null =>cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, config)
          case _ => cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, 5,pwd,config)
        }
      cluster.set(key, value)
    } catch {
      case e: Throwable => e.printStackTrace()
    } finally {
      //关连接池
      cluster.close()
    }
  }

  def psetex(key: String,ssl:Long,value: String): Unit = {
    try {
      val pwd:String = properties.getProperty(PropertiesConstant.REDIS_PASSWORD)
      pwd match {
        case null =>cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, config)
        case _ => cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, 5,pwd,config)
      }
      cluster.psetex(key,ssl,value)
    } catch {
      case e: Throwable => e.printStackTrace()
    } finally {
      //关连接池
      cluster.close()
    }
  }

  def get(key: String):String= {
    try {
      val pwd:String = properties.getProperty(PropertiesConstant.REDIS_PASSWORD)
      pwd match {
        case null =>cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, config)
        case _ => cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, 5,pwd,config)
      }
      cluster.get(key)
    } catch {
      case e: Throwable => {
       e.printStackTrace()
      "redis连接异常"
      }

    } finally {
      //关连接池
      cluster.close()
    }
  }

  def exists(key: String):Boolean= {
    try {
      val pwd:String = properties.getProperty(PropertiesConstant.REDIS_PASSWORD)
      pwd match {
        case null =>cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, config)
        case _ => cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, 5,pwd,config)
      }
      cluster.exists(key)
    } catch {
      case e: Throwable => {
        e.printStackTrace()
        false
      }
    } finally {
      //关连接池
      cluster.close()
    }
  }

  def del(key: String):Boolean= {
    try {
      val pwd:String = properties.getProperty(PropertiesConstant.REDIS_PASSWORD)
      pwd match {
        case null =>cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, config)
        case _ => cluster = new JedisCluster(nodes,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt,properties.getProperty(PropertiesConstant.REDIS_TIMEOUT).toInt, 5,pwd,config)
      }
      cluster.del(key)
      true
    } catch {
      case e: Throwable => {
        e.printStackTrace()
        false
      }
    } finally {
      //关连接池
      cluster.close()
    }
  }
}
